package cn.xyinping.quartz.job;
import cn.xyinping.order.domain.OrderPet;
import cn.xyinping.order.domain.OrderPetDetail;
import cn.xyinping.order.mapper.OrderPetDetailMapper;
import cn.xyinping.order.mapper.OrderPetMapper;
import cn.xyinping.pay.domain.PayBill;
import cn.xyinping.pay.mapper.PayBillMapper;
import cn.xyinping.pet.domain.Pet;
import cn.xyinping.pet.mapper.PetMapper;
import cn.xyinping.quartz.util.QuartzUtil;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.common.models.AlipayTradeCloseResponse;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 27266
 * @desrciption 订单超时处理
 * @date 2021/11/11
 */
// 交给容器
@Component
public class OrderExpireJob implements Job {
    @Autowired
    private PayBillMapper payBillMapper ;

    @Autowired
    private OrderPetDetailMapper orderPetDetailMapper ;

    @Autowired
    private OrderPetMapper orderPetMapper ;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private Scheduler scheduler;

    @Override
    @Transactional
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //获取数据
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        //数据
        Map<String,String> dataMap = (Map<String, String>) jobDataMap.get("params");
        String orderNo = dataMap.get("orderNo");
        // 查询订单支付单
        PayBill payBill = payBillMapper.loadByorderNo(orderNo);

        String jobName = dataMap.get("jobName");
        //* 2.如果状态不是待支付就不要处理，只是处理待支付
        if(payBill.getState().intValue() != PayBill.SATET_WAIT_PAY){
            QuartzUtil.removeJob(scheduler, jobName);
            System.out.println("删除定时任务...");
            return;
        }
        OrderPet orderPet = orderPetMapper.loadByOrderNo(orderNo);
        if(orderPet.getState().intValue() != OrderPet.STATE_WAIT_PAY ){
            QuartzUtil.removeJob(scheduler, jobName);
            System.out.println("删除定时任务...");
            return ;
        }

        //修改状态为超时
        payBill.setState(PayBill.SATET_CANCEL);
        Date date = new Date();
        payBill.setUpdateTime(date);
        payBillMapper.update(payBill);

        //* 3.查询业务单，修改为超时
        orderPet.setState(OrderPet.STATE_PAY_OUTTIME_CANCEL);
        orderPet.setLastUpdateTime(date);
        orderPetMapper.update(orderPet);

        //* 4.查询宠物，退库存
        List<OrderPetDetail> details = orderPetDetailMapper.loadByOrderNo(orderNo);
        //宠物ID集合
        List<Long> petIds = details.stream()
                .map(detail -> detail.getPetId()).collect(Collectors.toList());

        List<Pet> pets = petMapper.findByIds(petIds, Pet.STATE_ONLINE);
        pets.forEach(pet -> {
            //这里的1按道理应该是加：OrderPetDetail中的数量
            pet.setStore_number(pet.getStore_number()+1);
            petMapper.update(pet);
        });

        //* 5.调用支付宝关单接口
        try {
            AlipayTradeCloseResponse response = Factory.Payment.Common().close(orderNo);
            System.out.println("支付宝关单："+response);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //* 6.删除定时任务
        QuartzUtil.removeJob(scheduler, jobName);
        System.out.println("删除定时任务...");

    }
}
